/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.dao;

import com.tkgarment.system.db.Database;
import com.tkgarment.system.domain.EmployeeType;
import com.tkgarment.system.factory.DAOFactory;
import com.tkgarment.system.inf.IDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zraj
 */
public class EmpTypeDAO extends AbstractDAO implements IDAO<EmployeeType, Integer> {

    public EmpTypeDAO(Database con) {
        this.setCon(con);
    }

    public EmpTypeDAO() {
    }
    
    public List<EmployeeType> getAll()throws Exception
    {
        String sql = "SELECT emp_type_id,emp_type_name FROM emp_type ORDER BY emp_type_name ASC";
        this.setCon(DAOFactory.creatConnection());
        List<Map<String,Object>> result = this.getCon().queryList(sql);
        List<EmployeeType> empTypes = new ArrayList<>();
        for (Map<String, Object> map : result) {
            EmployeeType empType = new EmployeeType();
            empType.setEmpTypeID(Integer.parseInt(map.get(empType.field_empTypeID).toString()));
            empType.setEmpTypeName(map.get(EmployeeType.field_empTypeName).toString());
            empTypes.add(empType);
        }
        return empTypes;
    }


    @Override
    public int updateSingle(EmployeeType t, Integer id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int deleteSingle(EmployeeType t) throws Exception {
       String sql = "delete from emp_type where emp_type_id = ?";      
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        String checkUsed = "select * from emp_master where emp_type_id = ?";
        
        List<Map<String , Object>> result = this.getCon().queryList(checkUsed, t.getEmpTypeID());
        if (result.size() > 0) {
            return 99;
        }else
        {
          return this.getCon().remove(sql, t.getEmpTypeID());
        }
    }

    @Override
    public int insertSingle(EmployeeType t) throws Exception {
       String sql = "insert into emp_type(emp_type_name) values(?)";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        return this.getCon().add(sql,t.getEmpTypeName());
        
    }

    public EmployeeType getEmpTypeByID(Integer empTypeID) {
        String sql = "select * from emp_type where emp_type_id = ?";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        Map<String, Object> result = this.getCon().querySingle(sql, empTypeID);
        
        EmployeeType empType = new EmployeeType();
        
        empType.setEmpTypeID(empTypeID);
        empType.setEmpTypeName(result.get(EmployeeType.field_empTypeName).toString());
        
              
        return empType;
        
    }

    @Override
    public EmployeeType getById(Integer id) throws Exception {
        return getEmpTypeByID(id);
    }
    
    
}
